using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Threading;

using tessnet2;

namespace ConsoleTester
{
	public class Program
	{
		private void Run(string[] args)
		{
			try
			{
				using (Bitmap bmp = new Bitmap(@"c:\temp\w uma 3.png"))
				{
					Tesseract tess = new Tesseract();
					tess.Init("eng", "eng", false);

					List<Word> words = tess.DoOCR(
						bmp, 
						new Rectangle(
							(int)(bmp.Width  * 0.25),
							0,
							(int)(bmp.Width  * 0.50),
							(int)(bmp.Height * 0.10)));


				}
			}
			catch (Exception err)
			{
				Console.Error.WriteLine("Exception Details:\n{0}", err.ToString());
			}
		}

		public static void Main(string[] args)
		{
			Program program = new Program();
			program.Run(args);

			Console.Out.WriteLine("Press [ENTER] to quit...");
			Console.In.ReadLine();
		}

		public class Ocr
		{
			public void DumpResult(List<tessnet2.Word> result)
			{
				foreach (tessnet2.Word word in result)
					Console.WriteLine("{0} : {1}", word.Confidence, word.Text);
			}

			public List<tessnet2.Word> DoOCRNormal(Bitmap image, string lang)
			{
				tessnet2.Tesseract ocr = new tessnet2.Tesseract();
				ocr.Init(null, lang, false);
				List<tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty);
				DumpResult(result);
				return result;
			}

			ManualResetEvent m_event;

			public void DoOCRMultiThred(Bitmap image, string lang)
			{
				tessnet2.Tesseract ocr = new tessnet2.Tesseract();
				ocr.Init(null, lang, false);
				// If the OcrDone delegate is not null then this'll be the multithreaded version
				ocr.OcrDone = new tessnet2.Tesseract.OcrDoneHandler(Finished);
				// For event to work, must use the multithreaded version
				ocr.ProgressEvent += new tessnet2.Tesseract.ProgressHandler(ocr_ProgressEvent);
				m_event = new ManualResetEvent(false);
				ocr.DoOCR(image, Rectangle.Empty);
				// Wait here it's finished
				m_event.WaitOne();
			}

			public void Finished(List<tessnet2.Word> result)
			{
				DumpResult(result);
				m_event.Set();
			}

			void  ocr_ProgressEvent(int percent)
			{
 				Console.WriteLine("{0}% progression", percent);
			}
		}
	}
}
